package cn.tedu.v4;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 聊天室  Version3.1
 *
 */
public class Client {
    private static  final String HOST="192.168.59.17";
    private static  final Integer PORT=1234;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;
    private String severMessage;
    public Client(){
        try {
            socket=new Socket(HOST,PORT);
            ip=socket.getInetAddress().getHostAddress();
            br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
            pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
            severMessage=br.readLine();
        }catch (SocketException e){
            System.out.println();
        } catch(UnknownHostException e){
            System.out.println("IP地址有问题 请询问服务器端");
        } catch (IOException e) {
            System.out.println("客户端主动退出：断开连接");
        }
    }
    public void start(){
        try {
            System.out.println("~~~~欢迎使用皮皮牌聊天室~~~~");
            new Thread(() -> {
                try {
                    String message;
                    while ((message = br.readLine()) != null) {
                        System.out.println( message);
                        System.out.println("客户端请输入："+message);
                    }
                } catch (UnsupportedEncodingException e) {
                    System.out.println();
                } catch (SocketException e) {
                    System.out.println();
                } catch (IOException e) {
                    System.out.println("服务器端发来的消息断开了");
                }
            }).start();
            //System.out.println();
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.println(severMessage);
                String password=scanner.nextLine();
                pw.println(password);
                String username=scanner.nextLine();
                pw.println(username);

                String say=scanner.nextLine();
                pw.println("客户端说："+say);
            }
        }finally {
            if (br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯失败");
                }
            if (pw!=null)
                pw.close();
            if (socket!=null&&!socket.isClosed()){
                System.out.println("客户端关闭成功");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }

        }


        //读入
      /*  while (true){
            String str=br.readLine();
            System.out.println(str);
        }*/
    }




    public static void main(String[] args)  {
       new Client().start();
   }
}
